package com.railwayservice.user.dao;

import com.railwayservice.user.entity.User;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * 用户数据库访问接口
 *
 * @author lid
 * @date 2017.2.4
 */
public interface UserDao extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {

    /**
     * 根据id查找用户信息
     *
     * @param id
     * @return User
     * @author lid
     * @date 2017.2.9
     */
    User findUserByUserId(String id);

    /**
     * 根据openid查找对应的用户
     *
     * @param openid
     * @return User
     * @author lid
     * @date 2017.2.4
     */
    User findByOpenid(String openid);

    /**
     * 查询邀请人数大于inviteCount的用户
     */
    User findByInviteCountGreaterThan(Integer inviteCount);

    @Query("select u from User u where u.inviteCount > 0"
    		+ " and (:param is null or length(:param) = 0 or"
    		+ " u.nickName like :param or u.phoneNo like :param or u.openid like :param)")
	Page<User> queryPromotion(@Param("param") String param, Pageable pageable);
    
    @Query("select u from User u where u.inviteCount > 0"
    		+ " and (:param is null or length(:param) = 0 or"
    		+ " u.nickName like :param or u.phoneNo like :param or u.openid like :param)")
	List<User> queryPromotion(@Param("param") String param);
    
    /**
     * 查询被邀请的用户
     * @param userId
     * @return
     * @author lid
     */
    @Query("select u from User u where u.inviteUser like :openid order by createDate desc")
    List<User> findByInviteUser(@Param("openid")String openid);
}
